﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts.Interfaces;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class Tie : VehicleElement
    {
        #region Properties

        public string NodeId { get; set; }

        public float MaxLength { get; set; }

        public float Rate { get; set; }

        public float Short { get; set; }

        public float Long { get; set; }

        public string Group { get; set; }

        public long MaxForce { get; set; }

        #endregion

        #region CTORs

        public Tie()
        {
            MaxLength = float.MinValue;
            Rate = float.MinValue;
            Short = float.MinValue;
            Long = float.MinValue;
            MaxForce = long.MinValue;
        }

        public Tie(string nodeId)
            : this()
        {
            NodeId = nodeId;
        }

        public Tie(string nodeId, float maxLen, float rate, float shrt, float lng, string group, long maxForce)
            : this(nodeId)
        {
            MaxLength = maxLen;
            Rate = rate;
            Short = shrt;
            Long = lng;
            Group = group;
            MaxForce = maxForce;
        }

        #endregion

        #region Methods

        public override IVehicleElement FromJBeam(JToken token)
        {
            var arr = token as JArray;
            if (arr == null) return this;

            NodeId = token[0].FromJBeam<string>();
            MaxLength = token[1].FromJBeam<float>();
            Rate = token[2].FromJBeam<float>();
            Short = token[3].FromJBeam<float>();
            Long = token[4].FromJBeam<float>();
            Group = token[5].FromJBeam<string>();
            MaxForce = token[6].FromJBeam<long>();

            return this;
        }

        public override JToken ToJBeam()
        {
            return new JArray { NodeId, MaxLength, Rate, Short, Long, Group, MaxForce };
        }

        public override string ToString()
        {
            return string.Format("Linked node: {0}", NodeId);
        }

        #endregion
    }
}
